<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\Show;
use yii\data\Pagination;
use yii\db\ActiveRecord;
use yii\helpers\Url;
use frontend\models\UserFavourite;
use frontend\models\LogUserFootprint;

class ShowHomeController extends Controller{
    private function getSideBar()
    {
        $category_style=ActiveRecord::findBySql("SELECT DISTINCT grp_style.`name`,grp_style.id FROM `show_style`,grp_style
            WHERE show_style.style_id=grp_style.id ORDER BY grp_style.`order`")->asArray()->all();
        $category_color=ActiveRecord::findBySql("SELECT DISTINCT grp_color.id,grp_color.`name` FROM `show_color`,grp_color
            WHERE show_color.color_id=grp_color.id;")->asArray()->all();
        $hot_artical=ActiveRecord::findBySql("SELECT `show`.id,`show`.`name`,`show`.publish_time,`show`.content,sys_image.url
            FROM `show`,sys_image WHERE `show`.id >= (SELECT floor(RAND() * (SELECT MAX(`show`.id) FROM `show`)))
            AND `show`.app_return=1 AND `show`.img_small_id=sys_image.id ORDER BY id LIMIT 3;")->asArray()->all();
        return [
            'category_style'=>$category_style,
            'category_color'=>$category_color,
            'hot_artical'=>$hot_artical
        ];
    }
    
    public function actionIndex()
    {
        $pageSize=10;

        $state="";
        $color="";
        $style="";
        if(Yii::$app->request->get('state')||Yii::$app->request->get('state')=='0')
        {
            $state="AND `show`.state=".Yii::$app->request->get('state');
        }
        elseif (Yii::$app->request->get('color'))
        {
            $color="AND show_color.color_id=".Yii::$app->request->get('color');
        }
        elseif (Yii::$app->request->get('style'))
        {
            $style="AND show_style.style_id=".Yii::$app->request->get('style');
        }
        //Yii::$app->request->get('state')?$state="AND `show`.state=".Yii::$app->request->get('state'):$state="";
        //Yii::$app->request->get('color')?$color="AND show_color.color_id=".Yii::$app->request->get('color'):$color="";
        //Yii::$app->request->get('style')?$style="AND show_style.style_id=".Yii::$app->request->get('style'):$style="";
        
        $count=Show::findBySql('SELECT distinct `show`.id 
            FROM `show` LEFT JOIN sys_image ON sys_image.id=`show`.img_small_id 
            LEFT JOIN show_color ON `show`.id=show_color.show_id 
            LEFT JOIN show_style ON show_style.show_id=`show`.id 
            WHERE `show`.app_return=1 '.$state.' '.$color.' '.$style)->asArray()->count();
        
        $pagination = new Pagination([
            "defaultPageSize" => $pageSize,
            "totalCount"=>$count
        ]);
        
        $showModel=Show::findBySql('SELECT distinct `show`.id,`show`.`name`,`show`.state,`show`.publish_time,`show`.content,sys_image.url 
            FROM `show` LEFT JOIN sys_image ON sys_image.id=`show`.img_small_id 
            LEFT JOIN show_color ON `show`.id=show_color.show_id 
            LEFT JOIN show_style ON show_style.show_id=`show`.id 
            WHERE `show`.app_return=1 '.$state.' '.$color.' '.$style.' ORDER BY `show`.id DESC 
            LIMIT '.$pagination->offset.','.$pagination->limit)->asArray()->all();
        
        $SideBar=$this->getSideBar();
        
        return $this->render("showList",[
            'showModel'=>$showModel,
            'pagination'=>$pagination,
            'category_style'=>$SideBar['category_style'],
            'category_color'=>$SideBar['category_color'],
            'hot_artical'=>$SideBar['hot_artical'],
        ]);
    }
    public function actionShowHomeItem()
    {
        $hasAddFavourite=false;
        $show_id=intval(Yii::$app->request->get("show_id"));
        if (!Yii::$app->user->isGuest)
        {
            $user_id=Yii::$app->user->identity->id;
            //记录足迹
            $userFoot = new LogUserFootprint();
            $userFoot -> uid = $user_id;
            $userFoot -> calltime = time();
            $userFoot -> action = 'show';
            $userFoot -> action_id = Yii::$app->request->get('show_id');
            $userFoot -> title = $userFoot -> show0 ->name;
            $userFoot -> save();
            //记录足迹结束
            $res=UserFavourite::findOne(['uid'=>$user_id,'type_id'=>$show_id,'type'=>2]);
            if (!empty($res))
            {
                $hasAddFavourite=true;
            }
        }
        
        $sql="SELECT `show`.id,`show`.state,`show`.`name`,`show`.publish_time,admin_info.nickname,`show`.content,sys_image.url AS large_img,sys_image.width,sys_image.height 
            FROM `show` LEFT JOIN admin_info ON `show`.publish_uid=admin_info.id LEFT JOIN sys_image ON `show`.img_url_id=sys_image.id WHERE `show`.app_return=1 AND `show`.id=".$show_id;
        $show_res=ActiveRecord::findBySql($sql)->asArray()->all();
        if (empty($show_res))
        {
            return header("Location:".Url::to(['show-home/index']));
        }
        $pos_res=NUll;
        if ($show_res[0]['state']==0)
        {
            $sql="SELECT * FROM `show_pos` WHERE sid=".$show_id;
            $pos_res=ActiveRecord::findBySql($sql)->asArray()->all();
        }
        $SideBar=$this->getSideBar();
        return $this->render("showHomeItem",[
            'show_id'=>$show_id,
            'hasAddFavourite'=>$hasAddFavourite,
            'type'=>$show_res[0]['state'],
            'originHeight'=>$show_res[0]['height'],
            'originWidth'=>$show_res[0]['width'],
            'pos'=>$pos_res,
            'artical'=>$show_res[0],
            'hot_artical'=>$SideBar['hot_artical'],
        ]);
    }
}
